<?php
class WatchDog extends Object
{
	public static $path = "";

	public static $systemErrContent = "";
	public static $systemOutContent = "";
	public static $systemSqlContent = "";


	public static function writeFile($fileName,$content)
	{
		file_put_contents($fileName, $content, FILE_APPEND);
		//$fp = fopen($fileName,"a");
		//fwrite($fp,$content);
		//fclose($fp);
	}
	
	public static function screen_sql( $die_sql_execution=false )
	{
		define('WATCH_DOG_SCREEN', true);
		define('DATABASE_SQL_WATCHDOG', true);
		define('DATABASE_SQL_WATCHDOG_DIE', $die_sql_execution);
	}

	public static function makeFolder($prefix)
	{
		$helper = Helper::instance("File");
		$tmpPath = str_replace("{prefix}",$prefix,self::$path);
		if(!is_dir($tmpPath)) {
			$helper->createFolder($tmpPath);
		}
	}

	public static function setPath()
	{
		self::$path = PATH_LOG."{prefix}".SEPARATOR.date('Y').SEPARATOR.date('m').SEPARATOR;
	}

	static function getValidFile($prefix,$fileName="")
	{
		if(!self::$path) {
			self::setPath($prefix);
		}

		if($fileName)
		{
			$tmp = explode("_",$fileName);
			$counts = count($tmp);
			$tmpId = $tmp[$counts-1];
			$tmpId = explode('.',$tmpId);
			$tmpId = $tmpId[0];
			$tmp[$counts-1] = $tmpId+1;
			$tmp[$counts-1] .='.log';
			$fileName = implode("_",$tmp);
		} else {
			$fileName = $prefix.'_'. date('m').'_'.date('d').'_1.log';
		}
		$path = str_replace("{prefix}",$prefix,self::$path);
		$filePath = $path.$fileName;
		if(is_file($filePath))
		{
			if(filesize($filePath)>LOGFILESIZE)
			{
				return self::getValidFile($prefix,$fileName);
			}else{
				return $filePath;
			}
		} else {
			return	$filePath;
		}
	}


	public static function write()
	{
		if(IF_WEB_LOG)
		{
			self::setPath();
			if(self::$systemErrContent!="")
			{
				self::makeFolder(SYSTEMERR);
				$tmpFileName = self::getValidFile(SYSTEMERR);
				self::writeFile($tmpFileName,self::$systemErrContent);
			}
			if(self::$systemOutContent!="")
			{
				self::makeFolder(SYSTEMOUT);
				$tmpFileName = self::getValidFile(SYSTEMOUT);
				self::writeFile($tmpFileName,self::$systemOutContent);
			}
			if(self::$systemSqlContent!="")
			{
				self::makeFolder(SYSTEMSQL);
				$tmpFileName = self::getValidFile(SYSTEMSQL);
				self::writeFile($tmpFileName,self::$systemSqlContent);
			}
		}
	}


	public static function addSystemErr($str)
	{
		self::$systemErrContent .= $str.chr(13);
	}

	public static function addSystemOut($str)
	{
		self::$systemOutContent .= $str.chr(13);
	}

	public static function addSystemSql($str)
	{
		self::$systemSqlContent .= $str.chr(13);
	}
	
	public static function log( $file, $array_content, $glue='	')
	{
		$str = '';
		$count = count($array_content);
		$position = 0;
		
		for($i=0; $i<$count; $i++) {
			$str .= implode($glue, $array_content[$i]).chr(13).chr(10);
			
			$position++;
			if( $position % 50 == 0){
				self::writeFile($file, $str);
				$str = '';
			}
		}
		
		self::writeFile($file, $str);
	}
	
	public static function log_single( $file, $array_content, $glue='	')
	{
		self::writeFile($file, implode($glue, $array_content).chr(13).chr(10));
	}
	
	public static function record($str, $type='')
	{
		$string = date("Y-m-d H:i:s").chr(13).chr(10)
				.'--------------------------'.chr(13).chr(10)
				.self::getstring($str).chr(13).chr(10);
		if( $type ) {
			$type = $type.'_';
		}
		if( defined('WATCH_DOG_SCREEN') && WATCH_DOG_SCREEN == true){
			echo'<pre>'.$string.'</pre>';
			return true;
		}
		self::writeFile(WEBROOT.'log'.DIRECTORY_SEPARATOR.$type.date('Y_m_d_H').'.txt',$string);
	}
	
	public static function getstring($str)
	{
		$content = '';
		if(is_array($str)){
			foreach($str as $key=>$val){
				$content .= $key .': '. self::getstring($val).chr(13).chr(10);
			}
		} else {
			$content = $str;
		}
		
		return $content;
	}
}
?>